﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using Point = System.Drawing.Point;


namespace DragAndDrop
{
    class MoveWPFControlHelper
    {

        private static MoveControlHelper.MoveDirection _direction;
        static bool _dragging = false;
        static Point _dragStart = Point.Empty;
      
        public static void Initiallize(Control control)
        {
            Initiallize(control, MoveControlHelper.MoveDirection.Any);
        }

        public static void Initiallize(Control control, MoveControlHelper.MoveDirection direction)
        {
            Initiallize(control, control, direction);
        }

        public static void Initiallize(Control control, Control container, MoveControlHelper.MoveDirection direction)
        {

            _direction = direction;
            control.PreviewMouseDown += ControlMouseDown;
            control.PreviewMouseUp += ControlMouseUp;
            control.PreviewMouseMove += ControlMouseMove;
          
        }

        static void ControlMouseMove(object sender, MouseEventArgs e)
        {
            var container = (Control) sender;
            if (!_dragging) return;
            if (_direction != MoveControlHelper.MoveDirection.Y)
                container.Margin = new Thickness( e.GetPosition(container).X + container.Margin.Left - _dragStart.X, container.Margin.Top, container.Margin.Right, container.Margin.Bottom);
            if (_direction != MoveControlHelper.MoveDirection.X)
                container.Margin = new Thickness(container.Margin.Left, e.GetPosition(container).Y + container.Margin.Top - _dragStart.Y, container.Margin.Right, container.Margin.Bottom);
            container.BringIntoView();
            //zindex++;
            //Grid.SetZIndex(container,zindex);
        }

        private static int zindex = 0;

        public static T FindVisualParent<T>(DependencyObject obj)
       where T : DependencyObject
        {
            DependencyObject parent = VisualTreeHelper.GetParent(obj);
            while (parent != null)
            {
                T typed = parent as T;
                if (typed != null)
                {
                    return typed;
                }
                parent = VisualTreeHelper.GetParent(parent);
            }
            return null;
        }

static void  ControlMouseUp(object sender, MouseButtonEventArgs e)
{
 	  _dragging = false;
}



        static void ControlMouseDown(object sender, MouseButtonEventArgs e)
            {

                var current = (Control)sender;
                var foo = FindVisualParent<ContentPresenter>(current);
                _dragging = true;
                _dragStart = new Point((int)e.GetPosition((Control)sender).X, (int)e.GetPosition((Control)sender).Y);
                zindex++;
                Grid.SetZIndex(current, zindex);
            }
    }
}
